#ifndef NETWORK_H
#define NETWORK_H

#include <QObject>
#include <QtNetwork>
#include "Structs.h"

#define DEFAULT_PORT 57442

class Network : public QObject
{
    Q_OBJECT
    public:
        Network(QObject* p = 0, const QString& serverIP = "127.0.0.1", quint32 serverPort = DEFAULT_PORT);

    public slots:

    void connexion();
    void disconnect();

    void requestNextPhase();
    void requestPlayerInfos(quint32 player);
    void requestStateInfos(QString state);
    void requestUpdate();
    void requestMovement(QString from, QString to, quint32 nbr);
    void requestAttack(QString from, QString to, quint32 nbr, bool a);
    void requestReinforcement(QString state, quint32 nbr);

    void changeLobbyState();

    private slots:

    void dataReceived();

    void connected();
    void disconnected();

    void socketError(QAbstractSocket::SocketError err);

    void send();

    signals:
        void connecting();

        void connexionEtablished();
        void connexionLost();

        void phaseChanged(PHASE phase, quint32 playerID);
        void playerInfos(PlayerInfos infos);
        void stateInfos(StateInfos infos);
        void allPlayersInfos(QList<PlayerInfos> infos);
        void allStatesInfos(QList<StateInfos> infos);
        void error(ERROR_CODE errorCode, QStringList args);
        void info(INFOS_CODE infoCode, QStringList args);
        void requestDiceRoll(bool defensor);
        void playerDefeated(quint32 playerID);
        void gameStarted(quint32 nbrPlayers);

        void lobbyStateChanged(bool ready);

    private:

        QString m_serverIP;
        quint32 m_serverPort;

        QTcpSocket* m_socket;

        quint32 m_size;

        QQueue <Data*> m_toSend;
};

#endif
